import BButton from "./BButton";

const { ccclass, property } = cc._decorator;

@ccclass
export default class StateButton extends BButton {

    @property(cc.Boolean)
    clickEnable: boolean = true;

    @property(cc.SpriteFrame)
    defaultFrame: cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    activeFrame: cc.SpriteFrame = null;

    private sprite: cc.Sprite = null;
    private changeEvent: Function = null;
    private changeEventThisObj: any = null;
    private groupName: string = null;
    private static groups: { [key: string]: Array<StateButton> } = {};

    onTouchDown(e: cc.Event.EventTouch) {
        if (!this.enabled || !this.clickEnable) {
            return;
        }
        this.tryGetSprite();
        this.on = !this.on;
        super.onTouchDown(e);
    }

    onTouchUp(e: cc.Event.EventTouch) {
        if (!this.enabled || !this.clickEnable) {
            return;
        }
        super.onTouchUp(e);
    }

    setChangeEvent(func: Function, thisObj?: any) {
        this.changeEvent = func;
        this.changeEventThisObj = thisObj;
    }

    set on(b: boolean) {
        this.tryGetSprite();
        this.sprite.spriteFrame = b ? this.activeFrame : this.defaultFrame;
        if (this.changeEvent) {
            this.changeEvent.call(this.changeEventThisObj, this.on, this.index);
        }
        if (b) {
            let btns = StateButton.groups[this.groupName];
            if (btns) {
                for (let i = 0; i < btns.length; i++) {
                    if (btns[i] != this) {
                        btns[i].on = false;
                    }
                }
            }
        }
    }

    get on() {
        this.tryGetSprite();
        return this.sprite.spriteFrame == this.activeFrame;
    }

    private tryGetSprite() {
        if (!this.sprite) {
            this.sprite = this.getComponent(cc.Sprite);
        }
    }

    /**
     * @param group 给定相同的组名，具备单选按钮效果
     */
    static addStateButton(target: cc.Node, atlas: cc.SpriteAtlas, normalFrame: string, activeFrame: string, group?: string, index: number = 0): StateButton {
        if (!target) {
            console.error('target为null');
            return;
        }
        let btn = target.getComponent(StateButton);
        if (!btn) {
            btn = target.addComponent(StateButton);
        }
        btn.defaultFrame = atlas.getSpriteFrame(normalFrame);
        btn.activeFrame = atlas.getSpriteFrame(activeFrame);
        btn.index = index;
        btn.groupName = group;
        if (group) {
            let btns = StateButton.groups[group];
            if (btns) {
                let containsTarget: boolean = false;
                for (let i = 0; i < btns.length; i++) {
                    if (btns[i] == btn) {
                        containsTarget = true;
                        break;
                    }
                }
                if (!containsTarget) {
                    btns.push(btn);
                }
            } else {
                StateButton.groups[group] = [btn];
            }

        }
        return btn;
    }

}
